package model.basics;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import model.time.TimeFrame;

public class Request {
	
	private int capacity;
	private HashMap<Resource, Integer> resources;
	private List<TimeFrame> timeFrames;
	private Integer id;
	private Instructor instructor;
	private String course;
	private Boolean save;
	
	public String getCourse() {
		return course;
	}
	public void setCourse(String course) {
		this.course = course;
	}
	public Boolean getSave() {
		return save;
	}
	public void setSave(Boolean save) {
		this.save = save;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Request(int capacity, HashMap<Resource, Integer> resources,
			List<TimeFrame> timeFrames, int id, Instructor instructor, String course) {
		super();
		this.capacity = capacity;
		this.resources = resources;
		this.timeFrames = timeFrames;
		this.id = id;
		this.instructor = instructor;
		this.course=course;
	}
	public Request() {
		super();
		this.resources = new HashMap<Resource,Integer>();
		this.timeFrames = new Vector<TimeFrame>();
	}
	
	public Request(int i, HashMap<Resource, Integer> hmreq2,
			List<TimeFrame> tf1, int j, Instructor i3) {
		this.capacity = i;
		this.resources = hmreq2;
		this.instructor = i3;
		this.timeFrames = tf1;
		this.id = j;
	}
	public int getCapacity() {
		return capacity;
	}
	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}
	public HashMap<Resource, Integer> getResources() {
		return resources;
	}
	public void setResources(HashMap<Resource, Integer> resources) {
		this.resources = resources;
	}
	public List<TimeFrame> getTimeFrames() {
		return timeFrames;
	}
	public void setTimeFrames(List<TimeFrame> timeFrames) {
		this.timeFrames = timeFrames;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Instructor getInstructor() {
		return instructor;
	}
	public void setInstructor(Instructor instructor) {
		this.instructor = instructor;
	}
	public void addResource(Resource z) {
		if (this.getResources().containsKey(z)){
			this.getResources().put(z, this.getResources().get(z)+1);
		}
		else{
			this.getResources().put(z, 1);
		}
	}
	
	public String toString(){
		return ("Request "+this.getId());
	}
	
	public void addTimeFrame(TimeFrame t) {
		this.getTimeFrames().add(t);		
	}
	
	public void setResources(){
		Resource uno = new Resource("cosa0");
		Resource dos= new Resource("cosa1");
		Resource tres = new Resource("cosa2");
		this.resources.put(uno, 2);
		this.resources.put(dos, 1);
		this.resources.put(tres, 4);
		
		
	}
	
	
//	public void removeResource(Resource r){
//		resources.remove(r);
//		
//	}
	
	public void removeResource(Resource r){
		try{
			if(resources.containsKey(r)){
				resources.put(r, resources.get(r)-1);
				if(resources.get(r).equals(0)){
					resources.remove(r);
				}
			}
			else{
				throw new NoResourceException();
			}
		}
		catch (NoResourceException e) {
			e.printStackTrace();
		}
	}
	public void removeResource2(Resource r, int cant){
		try{
			if(resources.containsKey(r)){
				resources.put(r, resources.get(r)-cant);
				if(resources.get(r)<= 0){
					resources.remove(r);
				}
			}
			else{
				throw new NoResourceException();
			}
		}
		catch (NoResourceException e) {
			e.printStackTrace();
		}
	}
	
	public void print(){
		System.out.println(this.instructor);
		System.out.println(this.resources);
		this.timeFrames.get(0).print();
		System.out.println(this.capacity);
		System.out.println(this.id);
	}
	
	
	
	
	
	public String toShow(){
		String j = null;
		
		if(this.getTimeFrames().get(0).getType().equals("Weekly")){
			j = "Class day: "+this.getTimeFrames().get(0).getStringDay()+".";
		}
		if(this.getTimeFrames().get(0).getType().equals("Single")){
			j = "";
		}
		if(this.getTimeFrames().get(0).getType().equals("Daily")){
			j = "Class every day.";
		}
		if(this.getTimeFrames().get(0).getType().equals("Monthly")){
			j = "";
		}
		
		
		return (this.timeFrames.get(0).getType()+" time frame. \n\n Id request: "+this.getId()+"\n\n Instructor: "+this.getInstructor()+" \n\n Date start: "+this.timeFrames.get(0).getStringDateStart()+" "
				+" \n\n Date end: "+this.timeFrames.get(0).getStringDateEnd()+"\n\n Time start: " +this.timeFrames.get(0).getStringTimeStart()+
				"\n\n Time end: "+this.timeFrames.get(0).getStringTimeEnd()+"\n\n Classroom capacity: "+this.getCapacity()+"\n "+j+
				"\n\n Classroom resources: "+this.resourcesToString());
	}
	
	public void remResource(String r){
		//removes resources from a string
		Set set = this.resources.entrySet();
		Iterator it = this.resources.entrySet().iterator();
		while (it.hasNext()){
			Map.Entry e = (Map.Entry) it.next();
			if(e.getKey().toString().equals(r)){
				this.removeResource((Resource) e.getKey());
			}
		}
	}
	public void remVariousResources2(Vector<String> res, int cant){
		//removes various resources from strings
		Set set = this.resources.entrySet();
		Iterator it = this.resources.entrySet().iterator();
		while (it.hasNext()){
			Map.Entry e = (Map.Entry) it.next();
			for (String r: res){
			if(e.getKey().toString().equals(r)){
				this.removeResource2((Resource) e.getKey(), cant);
			}
			}
		}
	}
	public void remVariousResources(Vector<String> res){
		//removes various resources from strings
		Set set = this.resources.entrySet();
		Iterator it = this.resources.entrySet().iterator();
		while (it.hasNext()){
			Map.Entry e = (Map.Entry) it.next();
			for (String r: res){
				if(e.getKey().toString().equals(r)){
					this.removeResource((Resource) e.getKey());
				}
			}
		}
	}
	
	
	
	
	public List<Resource> resourcesToList(){
		Set set = this.resources.entrySet();
		Iterator it = this.resources.entrySet().iterator();
		Vector nueva = new Vector<Resource>();
		while (it.hasNext()){
			Map.Entry e = (Map.Entry) it.next();
			nueva.add(e);
		}
		return nueva;
		
	}
	
	public String resourcesToString(){
		String x ="";
		Set set = this.resources.entrySet();
		Iterator it = this.resources.entrySet().iterator();
		Vector nueva = new Vector<Resource>();
		while (it.hasNext()){
			Map.Entry e = (Map.Entry) it.next();
			x = x+" "+e.toString();
		}
		return x;
	}
	
	
}
